"""
绘制各类事件的测试类
"""


import iplot.iplothelper as iplot
from iplot.bar import EventResearch
import sqlite3

import datetime
import time
import numpy as np


# '%Y-%m-%d'
def dateStr2timestamp(t_str,date_format):
    d = datetime.datetime.strptime(t_str, date_format)
    return int(time.mktime(d.timetuple()))


def eventDraw():
    '''基本面事件绘制

    [description]
    '''
    start_date = '2016-01-08'
    end_date = '2016-01-14'
    start_timestamp = iplot.dateStr2timestamp(start_date,'%Y-%m-%d')
    end_timestamp = iplot.dateStr2timestamp(end_date,'%Y-%m-%d')
    candleObj = iplot.getdata_by_timestamp(start_timestamp,end_timestamp)
    # 绘制基本K线图
    plt, fig, ax = iplot.draw_candlestick_base(candleObj)
    # # 获取基本面事件
    eventlist = iplot.get_eventlist_bytimestamp(start_timestamp, end_timestamp)
    # # 绘制基本面事件
    iplot.draw_event(plt, fig, ax, candleObj, eventlist)

    plt.show()


def moveAverageLine():
    '''绘制移动平均线

    [description]
    '''
    start_date = '2016-01-08'
    end_date = '2016-01-14'
    start_timestamp = iplot.dateStr2timestamp(start_date,'%Y-%m-%d')
    end_timestamp = iplot.dateStr2timestamp(end_date,'%Y-%m-%d')
    candleObj = iplot.getdata_by_timestamp(start_timestamp,end_timestamp)
    # 绘制基本K线图
    plt, fig, ax = iplot.draw_candlestick_base(candleObj)

    ma10 = iplot.moving_average(candleObj.close_list, 10, 'simple')
    ma20 = iplot.moving_average(candleObj.close_list, 20, 'simple')

    ax.plot(candleObj.dateStr_list, ma10, color='c', lw=2, label='MA (10)')
    ax.plot(candleObj.dateStr_list, ma20, color='red', lw=2, label='MA (20)')

    plt.show()


def rangeDraw():
    '''绘制矩形示例

    [description]
    '''
    start_date = '2016-01-08'
    end_date = '2016-01-11'
    start_timestamp = iplot.dateStr2timestamp(start_date,'%Y-%m-%d')
    end_timestamp = iplot.dateStr2timestamp(end_date,'%Y-%m-%d')
    candleObj = iplot.getdata_by_timestamp(start_timestamp,end_timestamp)
    # 绘制基本K线图
    plt, fig, ax = iplot.draw_candlestick_base(candleObj)
    
    t = dateStr2timestamp('2016-01-08 00:01:00','%Y-%m-%d %H:%M:%S')
    t1 = dateStr2timestamp('2016-01-09 00:01:00','%Y-%m-%d %H:%M:%S')
    iplot.draw_rectangle(plt, fig, ax, candleObj, t, t1, '诸恶莫作')


    plt.show()


def event_observer():
    '''基本面事件观察

    [description]
    '''
    event_list = iplot.get_eventlist_byName('USD', '非农就业人数 ')
    l = len(event_list)
    e = event_list[-10]
    print(e.dateStr)
    ename = e.eventName
    event_timestamp = e.timestamp

    # 获取当日0点时间戳
    ob_start_timestamp = iplot.getZerotimestamp(event_timestamp)
    # 要往后看多少秒
    ob_end_timestamp = iplot.getNextDayZero_timestamp(event_timestamp, 1)
    print(iplot.timestamp2str(ob_end_timestamp))
    # 要绘制的矩形范围
    event_start_timestamp = event_timestamp
    event_end_timestamp = event_start_timestamp + 3600

    candleObj = iplot.getdata_by_timestamp(
        ob_start_timestamp, ob_end_timestamp)
    print('蜡烛图从 %s - %s' % (iplot.timestamp2str(
        candleObj.timestamp_list[0]), iplot.timestamp2str(candleObj.timestamp_list[-1])))
    if len(candleObj.timestamp_list) <= 0:
        print("查无数据")
        return
    # 绘制基本K线图
    plt, fig, ax = iplot.draw_candlestick_base(candleObj)
    iplot.draw_rectangle(fig, ax, candleObj,
                         event_start_timestamp, event_end_timestamp, ename)
    plt.show()
    # print(event_list)




if __name__ == '__main__':
    eventDraw()
    # rangeDraw()
    # e.plt.show()
    # print(e.index)
     #moveAverageLine()
    # event_observer()
    # moveAverageLine()
    # event_observer() 
    